<?php
/**
 * @author Oliver Leisalu
 * @package DomBankLink
 */
class DomBankLink_Dummy extends DomBankLink {
	
	/**
	* Private key.
	* @var String
	*/
	protected $privateKey;
	
	public function getName() {
		return 'Dummy';
	}
	
	public function getKey() {
		return 'Dummy';
	}
	
	public function getIconUrl() {
		return DOM_URL . '../libsPub/DomBankLink/images/Dummy.gif';
	}
	
	public function sendToBank( DomBankLinkObject $payment ) {
		if( $payment instanceof DomBankPayment ) {
			$html = '
				<center>
				<div style="font-family:Verdana; font-size:12px; text-align:center; margin-top:100px; border: 1px solid black; width:300px; padding:20px;">
				<form id="form" action="'.$this->getCallBackUrl( $payment ).'" method="POST">
					<b style="font-size:13px;">This is test bank only! No money will be actually transfered!</b><br>
					<br>
					Transfer: <b style="color:red">'.$payment->price.' '.strtoupper($payment->currency).'</b><br>
					Description: <b>'.$payment->description.'</b><br>
					<br>
					<table cellspacing="2" cellpadding="2" border="0">
						<tr>
							<td style="font-size:11px;">Person full name:</td>
							<td><input type="text" name="clientName" value="Sample name"  style="font-size:11px;"></td>
						</td>
						<tr>
							<td width="150" style="font-size:11px;">Personal code:</td>
							<td width="150"><input type="text" name="clientAccountNr" value="100000000000"  style="font-size:11px;"></td>
						</td>
					</table>
					<br>
					<input type="hidden" name="logId" value="'.$payment->id.'">
					<input type="hidden" name="price" value="'.$payment->price.'">
					<input type="hidden" name="fromDummy" value="dummy">
					<input type="hidden" name="mac" value="'.$this->calculateMac( $payment->id, $payment->price ).'">
					<input type="submit" name="successful" value="Pay" />
					<input type="submit" name="cancel" value="Cancel" />
					<input type="submit" name="error" value="Error" />
				</form>
				</div>
				</center>
				';
			echo $html;
			die();
		} else if( $payment instanceof DomBankIdentify ) {
			$html = '
			<center>
			<div style="font-family:Verdana; font-size:12px; text-align:center; margin-top:100px; border: 1px solid black; width:300px; padding:20px;">
			<form id="form" action="'.$this->getCallBackUrl( $payment ).'" method="POST">
				<b style="font-size:13px;">This is test bank only! </b><br>
				<br>
				<table cellspacing="2" cellpadding="2" border="0">
					<tr>
						<td width="150" style="font-size:11px;">Personal code:</td>
						<td width="150"><input type="text" name="personCode" value="31122334567"  style="font-size:11px;"></td>
					</td>
					<tr>
						<td style="font-size:11px;">Person full name:</td>
						<td><input type="text" name="personName" value="Sample name"  style="font-size:11px;"></td>
					</td>
				</table>
				<br>
				<input type="hidden" name="logId" value="'.$payment->id.'">
				<input type="hidden" name="price" value="0">
				<input type="hidden" name="fromDummy" value="dummy">
				<input type="hidden" name="mac" value="'.$this->calculateMac( $payment->id, 0 ).'">
				<input type="submit" name="successful" value="Identify" />
				<input type="submit" name="cancel" value="Cancel" />
			</form>
			</div>
			</center>
			';
		echo $html;
		die();
		}
	}
	
	public function handleReplay( $paymentClassName, $requestId = NULL ) {

		if( $_REQUEST['logId'] > 0 ) {
			$requestId =  $_REQUEST['logId'];
		}
		
		$mac = $this->calculateMac( $requestId, $_REQUEST['price'] );
		$payment = DomAr::load( $requestId, $paymentClassName );
		if( !$payment )
			return false;
		
		if( $payment->status != DomBankLinkObject::SENT_TO_BANK )
			return $payment;
		
		if( $mac != $_REQUEST['mac'] or $_REQUEST['error']) {
	
			$payment->status = 3;
		} else if( $_REQUEST['cancel']) {
			$payment->status = 2;
			$payment->handled = true;
		} else {
			$payment->status = 1;
		}
		
		$payment->backTime = 'now';
		$payment->backIp = ip();
		
		if( $payment instanceof DomBankPayment ) {
			$payment->clientAccountNr = $_REQUEST['clientAccountNr'];
			$payment->clientName = strtoupper($_REQUEST['clientName']);
		}
		
		if( $payment->status == DomBankLinkObject::OK && $payment instanceof DomBankIdentify ) {
			$payment->clientCode = $_REQUEST['personCode'];
			$payment->clientName = strtoupper($_REQUEST['personName']);
		}

		if( !$payment->save() ) {
			throw new RuntimeException( $payment->getValidationMessages('<br>') );
		}
		
		return $payment;
	}
	
	public function calculateMac( $callBackId, $price ){
		return md5(sha1(md5($this->privateKey.'|'.$callbackId.'|'.$price.'|dummy')));
	}
	
	
	
}
?>